package com.onjob.message;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import util.dao.HibernateDaoImpl;

public class ResumeRecruitDaoImpl extends HibernateDaoImpl<ResumeRecruit,String> implements
		ResumeRecruitDao {

	public void setUserReadTag(final String resumeRecruitId){
		StringBuffer hql = new StringBuffer();
		hql.append("update ResumeRecruit r set r.userVisible=1 where 1=1 ");
		hql.append("and r.id=:resumeRecruitId ");
		final String s = new String(hql);
		this.getHibernateTemplate().execute(
				new HibernateCallback(){
					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session.createQuery(s);
						query.setString("resumeRecruitId", resumeRecruitId);
						query.executeUpdate();
						return null;
					}
					
				}
		);
	}
	
	public void setCompanyReadTag(final String resumeRecruitId){
		StringBuffer hql = new StringBuffer();
		hql.append("update ResumeRecruit r set r.companyVisible=1 where 1=1 ");
		hql.append("and r.id=:resumeRecruitId ");
		final String s = new String(hql);
		this.getHibernateTemplate().execute(
				new HibernateCallback(){
					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session.createQuery(s);
						query.setString("resumeRecruitId", resumeRecruitId);
						query.executeUpdate();
						return null;
					}
					
				}
		);
	}
	@SuppressWarnings("unchecked")
	public List<ResumeRecruit> getResumeByCompany(final String companyId){
		
		StringBuffer hql = new StringBuffer();
		hql.append("from ResumeRecruit r where 1=1 ");
		hql.append("and r.recruitId.companyId.id=:companyId ");
		final String s = new String(hql);
		List<ResumeRecruit> list = (List<ResumeRecruit>)this.getHibernateTemplate().execute(
				new HibernateCallback(){

					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session.createQuery(s);
						query.setString("companyId", companyId);
						return query.list();
					}
					
				}
		);
		return list;
	}
	@SuppressWarnings("unchecked")
	public List<ResumeRecruit> getResumeByUser(final String userId){
		StringBuffer hql = new StringBuffer();
		hql.append("from ResumeRecruit r where 1=1 ");
		hql.append("and r.resumeId.personInfo.userId.id=:userId ");
		final String s = new String(hql);
		List<ResumeRecruit> list = (List<ResumeRecruit>)this.getHibernateTemplate().execute(
				new HibernateCallback(){

					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session.createQuery(s);
						query.setString("userId", userId);
						return query.list();
					}
					
				}
		);
		return list;
	}
}
